<template>
	<view style="padding-top: 80rpx;" :class="{'p_b_128': status === 1}">
		<view class="top_wrapper">
			<view class="p_font_28" @click="getTopChange">{{status === 0?'管理': '完成'}}</view>
		</view>
		<noneData noneTitle="暂无收藏~" v-if="listData.length === 0"></noneData>
		<view class="p_over" v-else>
			<view class="list_wrapper" v-for="item in listData" :key="item.id" @click="getDetail(item)">
				<image src="https://dy.sykj365.com/uploads/mini/weixuanzhong@2x.png" class="p_img_48 p_margin_r_24"
					v-if="status === 1 && !item.choose"></image>
				<image src="https://dy.sykj365.com/uploads/mini/xuanzhong@2x.png" class="p_img_48 p_margin_r_24"
					v-if="status === 1 && item.choose"></image>
				<view class="p_flex_2 p_flex">
					<image :src="item.image" mode="aspectFill" class="p_img_128 p_margin_r_16">
					</image>
					<view class="p_flex_2">
						<view class="p_font_28 p_two_text" style="max-height: 80rpx;">{{item.name}}</view>
						<view class="p_font_24 p_color_b2 p_margin_t_8">{{item.file_num}}个文件</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom_wrapper" v-if="status === 1">
			<view class="p_flex2" @click="getAllFun">
				<image src="https://dy.sykj365.com/uploads/mini/weixuanzhong@2x.png" class="p_img_48 p_margin_r_8" v-if="!isAllChoose">
				</image>
				<image src="https://dy.sykj365.com/uploads/mini/xuanzhong@2x.png" class="p_img_48 p_margin_r_8" v-else></image>
				<view class="p_font_24 p_color_7a">全选</view>
			</view>
			<view class="btn p_font_28 p_color_ff5555" @click="getDel">删除</view>
		</view>
		<!--  -->
		<tips messages="是否确定取消收藏？" confirmText="确定" cancelText="再想想" :tipsShow="detelMsg"
			@getDialogCancel="detelMsg = false" @getDialogConfirm="getDetel"></tips>
	</view>
</template>

<script>
	import tips from '../../components/tips.vue';
	import noneData from '../../components/noneData.vue';

	export default {
		components: {
			tips,
			noneData
		},
		data() {
			return {
				status: 0, // 控制页面 0-正常 1-删除
				listData: [],
				isAllChoose: false,
				detelMsg: false,
				colletIds: '',
			}
		},
		onLoad() {
			this.getListData();
		},
		onShow() {
			if (uni.getStorageSync('isCollectList')) {
				this.getListData();
				uni.removeStorageSync('isCollectList');
			}
		},
		methods: {
			getListData() {
				this.$http.post(this.$api.collectList).then(res => {
					if (res.data.length > 0) {
						res.data.map((n) => {
							n.choose = false;
							return n
						})
					}

					this.listData = res.data;

					if (res.data.length === 0) this.status = 0;
				})
			},
			getTopChange() {
				if (this.status === 0) {
					this.status = 1;
				} else {
					this.status = 0;
				}
			},
			getDetail(item) {
				if (this.status === 0) {
					getApp().getNavigateTo('/pages/dataBank/detail?collect=true&id=' + item.id);
				}
				if (this.status === 1) {
					item.choose = !item.choose;
					this.getIsAll();
				}
			},
			getIsAll() {
				let c_ = true;
				this.listData.forEach((n) => {
					if (!n.choose) c_ = false;
				})
				this.isAllChoose = c_;
			},
			getAllFun() {
				this.listData.forEach((n) => {
					if (this.isAllChoose) {
						n.choose = false;
						this.isAllChoose = false;
					} else {
						n.choose = true;
						this.isAllChoose = true;
					}
				})
			},
			getDel() {
				let ids = [];
				this.listData.forEach((n) => {
					if (n.choose) ids.push(n.id);
				})

				if (ids.length === 0) return getApp().getMsg('请勾选删除数据');

				this.detelMsg = true;
				this.colletIds = ids.join(',');
			},
			getDetel() {
				this.$http.post(this.$api.cancelCollect, {
					ids: this.colletIds
				}).then(res => {
					getApp().getMsg('已取消收藏');
					this.detelMsg = false;
					this.isAllChoose = false;
					this.getListData();
				})
			}
		}
	}
</script>

<style lang="less">
	.top_wrapper {
		width: 100%;
		height: 80rpx;
		background-color: #f3f3f3;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: fixed;
		top: 0;

		.p_font_28 {
			padding: 10rpx 10rpx;
			margin-right: 50rpx;
		}
	}

	.list_wrapper {
		display: flex;
		align-items: center;
		margin-bottom: 48rpx;
		padding: 0 32rpx;

		&:first-child {
			margin-top: 32rpx;
		}
	}

	.bottom_wrapper {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		padding: 18rpx 32rpx 38rpx;
		position: fixed;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.btn {
		width: 152rpx;
		height: 72rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #FF5555;
		box-sizing: border-box;
		border-radius: 56rpx;
	}
</style>
